/*    FreeSofwareCenter 
 *    Copyright (C) 2014  FreeSofwareTeam
 *	
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *    GNU General Public License for more details.
 *	
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package test;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DebugGraphics;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.ListModel;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import test.properties;

import org.jdesktop.application.Application;
import org.xml.sax.SAXException;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class main extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton buttonSecciones;
	private JList<Object> listCategory;
	private JScrollPane listCategoryContainer;
	private JButton jButton1;
	private static JTable tableSofware;
	private JScrollPane plano;
	private JButton jButton4;
	private JLabel TituloFiltro;
	private JTextField filtroRapido;
	private JTextPane sofwareDescritionTextarea;
	private JButton jButton8;
	private JButton jButton3;
	private JButton jButton2;
	private JButton Recargar;
	private JPanel panelSecundario;
	private ImageIcon images[];
	private TableRowSorter<TableModel> sorter;



	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				main inst = new main();
				inst.setLocationRelativeTo(null);
				try {
					resoreceServer.setSeverActive(resoreceServer.getServer1());
				} catch (XPathExpressionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				inst.setVisible(true);
		
			}
		});
	}
	
	public main() {
		super();
		initGUI();
	}
	
	
	  private void initGUI() {
		try {
			
			// inizializan las imagenes
			
			// directorio actual
			String curDir = System.getProperty("user.dir");
			
			// saver si un archivo existe
			//System.out.println(new File(curDir+"/img/sin instalar.png").exists());
			
			images=new ImageIcon[6];
			
			images[0] =new ImageIcon(curDir+"/img/sin instalar.png");
			images[1] =new ImageIcon(curDir+"/img/marcar para instalar.png");
			images[2] =new ImageIcon(curDir+"/img/marcar para reinstalar.png");
			images[3] =new ImageIcon(curDir+"/img/marcar para actualizar.png");
			images[4] =new ImageIcon(curDir+"/img/marcar para desintalar.png");
			images[5] =new ImageIcon(curDir+"/img/marcar para desintalar completamente.png");
			
			//
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				panelSecundario = new JPanel();
				GroupLayout panelSecundarioLayout = new GroupLayout((JComponent)panelSecundario);
				panelSecundario.setLayout(panelSecundarioLayout);
				panelSecundario.setName("panelSecundario");
				{
					Recargar = new JButton();
					Recargar.setName("Recargar");
					Recargar.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							try {
								RecargarMouseClicked(evt);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								
								  JOptionPane.showMessageDialog(null, "No se ha encontrado repositorio xml con los sofwares disponibles", "Error",
		                                    JOptionPane.ERROR_MESSAGE);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
				{
					jButton1 = new JButton();
					jButton1.setName("jButton1");
				}
				{
					jButton2 = new JButton();
					jButton2.setName("jButton2");
					jButton2.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							try {
								jButton2MouseClicked(evt);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
					jButton2.addKeyListener(new KeyAdapter() {
						public void keyPressed(KeyEvent evt) {
							jButton2KeyPressed(evt);
						}
					});
				}
				{
					jButton3 = new JButton();
					jButton3.setName("jButton3");
					jButton3.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jButton3MouseClicked(evt);
						}
					});
				}
				{
					filtroRapido = new JTextField();
					filtroRapido.setName("filtroRapido");
				}
				{
					TituloFiltro = new JLabel();
					TituloFiltro.setName("TituloFiltro");
				}
				{
					jButton4 = new JButton();
					jButton4.setName("jButton4");
					jButton4.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jButton4MouseClicked(evt);
						}
					});
				}
				panelSecundarioLayout.setHorizontalGroup(panelSecundarioLayout.createSequentialGroup()
					.addComponent(Recargar, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton3, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
					.addGap(20)
					.addGroup(panelSecundarioLayout.createParallelGroup()
					    .addComponent(filtroRapido, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
					    .addGroup(GroupLayout.Alignment.LEADING, panelSecundarioLayout.createSequentialGroup()
					        .addGap(22)
					        .addComponent(TituloFiltro, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					        .addGap(25)))
					.addGap(0, 26, Short.MAX_VALUE)
					.addComponent(jButton4, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(77, 77));
				panelSecundarioLayout.setVerticalGroup(panelSecundarioLayout.createParallelGroup()
					.addGroup(GroupLayout.Alignment.LEADING, panelSecundarioLayout.createSequentialGroup()
					    .addComponent(TituloFiltro, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					    .addComponent(filtroRapido, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addGap(0, 28, Short.MAX_VALUE))
					.addComponent(Recargar, GroupLayout.Alignment.LEADING, 0, 73, Short.MAX_VALUE)
					.addComponent(jButton1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
					.addComponent(jButton4, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE));
			}
			{
				sofwareDescritionTextarea = new JTextPane();
				sofwareDescritionTextarea.setName("sofwareDescritionTextarea");
			}
			{
				jButton8 = new JButton();
				jButton8.setName("jButton8");
			}
			{
				listCategoryContainer = new JScrollPane();
				{
					ListModel<Object> listCategoryModel = 
							new DefaultComboBoxModel<Object>(
									new String[] { "Todos", "Instalados","sin instalar"  });
					listCategory = new JList<Object>();
					listCategoryContainer.setViewportView(listCategory);
					listCategory.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							listCategoryMouseClicked(evt);
						}
					});
					listCategory.setModel(listCategoryModel);
					listCategory.setSelectedIndex(0);
				}
			}
			{
				plano = new JScrollPane();
				plano.getHorizontalScrollBar().setSize(560, 15);
				plano.getHorizontalScrollBar().setOpaque(true);
				plano.getHorizontalScrollBar().setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
				plano.getHorizontalScrollBar().setPreferredSize(new java.awt.Dimension(300, 15));
				plano.getHorizontalScrollBar().setLocation(new java.awt.Point(0, 245));
				plano.getVerticalScrollBar().setPreferredSize(new java.awt.Dimension(15, 5));
				plano.setSize(800, 247);
				plano.getHorizontalScrollBar().setVisible(true);
				plano.getVerticalScrollBar().setAutoscrolls(true);
				plano.getHorizontalScrollBar().setAutoscrolls(true);
				{
					
					
					 
					    
					    
					   
					final TableModel tableSofwareModel = 
							new DefaultTableModel(
								    
								     
									new Object[][] { { } },
									new String[] {"E"," ","Programa","Version Instalda","Ultima version","Descripcion","omitida" });
								
					tableSofware = new JTable(){	  
						/**
						 * 
						 */
						private static final long serialVersionUID = 1L;

						public boolean isCellEditable(int rowIndex, int vColIndex) {
				        return false;
				        }
				
					
					};
					
					// permite el ordenamiento de la tabla
					sorter= new TableRowSorter<TableModel>(tableSofwareModel);
					tableSofware.setRowSorter(sorter);
					// fin 
					
					
					plano.setViewportView(tableSofware);
					tableSofware.setModel(tableSofwareModel);
					
					tableSofware.setPreferredSize(new java.awt.Dimension(1000, 354));
					tableSofware.getColumnModel().getColumn(0).setMaxWidth(10);
					tableSofware.getColumnModel().getColumn(1).setMaxWidth(10);
					tableSofware.getColumnModel().getColumn(2).setMaxWidth(100);
					tableSofware.getColumnModel().getColumn(3).setMaxWidth(100);
					tableSofware.getColumnModel().getColumn(4).setMaxWidth(100);
					tableSofware.getColumnModel().getColumn(5).setMaxWidth(600);
					
					// oculta la ultima fila que tiene el estado anterior de la aplicacion 
					tableSofware.getColumnModel().getColumn(6).setMinWidth(0);
					tableSofware.getColumnModel().getColumn(6).setMaxWidth(0);
					tableSofware.getColumnModel().getColumn(6).setWidth(0);
					   
					tableSofware.setSize(561, 304);
					tableSofware.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
					tableSofware.setName("tableSofware");
					
					tableSofware.setShowGrid(false);
					tableSofware.setShowVerticalLines(false);
					tableSofware.setShowHorizontalLines(false);
					tableSofware.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
					resoreceServer.loadSofwareCenterList(tableSofware);
					tableSofware.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							tableSofwareMouseClicked(evt);
						}
					});

					;

				}
			}
			{
				buttonSecciones = new JButton();
				buttonSecciones.setName("buttonSecciones");
				buttonSecciones.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonSeccionesMouseClicked(evt);
					}
				});
			}
			thisLayout.setHorizontalGroup(thisLayout.createParallelGroup()
			.addComponent(panelSecundario, GroupLayout.Alignment.LEADING, 0, 836, Short.MAX_VALUE)
			.addGroup(thisLayout.createSequentialGroup()
			    .addGap(28)
			    .addGroup(thisLayout.createParallelGroup()
			        .addComponent(buttonSecciones, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
			        .addComponent(jButton8, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
			        .addComponent(listCategoryContainer, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE))
			    .addGap(32)
			    .addGroup(thisLayout.createParallelGroup()
			        .addComponent(sofwareDescritionTextarea, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 576, GroupLayout.PREFERRED_SIZE)
			        .addComponent(plano, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 579, GroupLayout.PREFERRED_SIZE))
			    .addContainerGap(56, Short.MAX_VALUE)));
		thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addComponent(panelSecundario, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
				.addGap(29)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(plano, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE)
				    .addComponent(listCategoryContainer, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 245, GroupLayout.PREFERRED_SIZE))
				.addGap(33)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(sofwareDescritionTextarea, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(buttonSecciones, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(jButton8, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 125, Short.MAX_VALUE)))
				.addContainerGap(56, 56));
			pack();
			this.setSize(852, 690);
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	

	
	private void RecargarMouseClicked(MouseEvent evt) throws Exception {
		System.out.println("Recargar.mouseClicked, event="+evt);
		//TODO add your code for Recargar.mouseClicked
		resoreceServer.refreshSofwareCenter(tableSofware);
	
	}
	public static void recargarNoButton() throws Exception{
		
		resoreceServer.refreshSofwareCenter(tableSofware);
	}
	
	
	private void tableSofwareMouseClicked(MouseEvent evt) {
		 System.out.println("llamo ");
		if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
			   System.out.println("Right Click");
		     // JPopUpMenu.show(myJTable, evt.getX(), evt.getY())
			   
			   resoreceServer.crearSeleccionDisponible(images, tableSofware,evt);
		        
		        
			
		}
		
	}
	
	private void buttonSeccionesMouseClicked(MouseEvent evt) {
		System.out.println("buttonSecciones.mouseClicked, event="+evt);
		//TODO add your code for buttonSecciones.mouseClicked
		
		
		//listCategory.setce
		
		ListModel<Object> listCategoryModel =new DefaultComboBoxModel<Object>(
			new String[] { "Todos", "Instalados","sin instalar"});
		
		listCategory.setModel(listCategoryModel);
		
	}
	
	private void jButton2KeyPressed(KeyEvent evt) {
		System.out.println("jButton2.keyPressed, event="+evt);
		//TODO add your code for jButton2.keyPressed
		
		
	
	}
	
	private void jButton2MouseClicked(MouseEvent evt) throws Exception {
		System.out.println("jButton2.mouseClicked, event="+evt);
		//TODO add your code for jButton2.mouseClicked
		
		
		int confirmado = JOptionPane.showConfirmDialog(
				   this,
				   "Esta seguro de confirmar los cambios","confirmar cambios",JOptionPane.YES_NO_OPTION);

				if (JOptionPane.OK_OPTION == confirmado)
				{
					System.out.println("confirmado");
					
					resoreceServer.aplicarCambios(images,tableSofware,this);
					
				}
				   
				else
				{
					System.out.println("cancelo los cambios");
				}
				   
		
	}
	
	private void listCategoryMouseClicked(MouseEvent evt) {
		System.out.println("listCategory.mouseClicked, event="+evt);
		//TODO add your code for listCategory.mouseClicked
		 if (evt.getClickCount() == 1) {
	            // single click
			 int selecciono=listCategory.getSelectedIndex();
			 System.out.println("selecciono:"+selecciono);	
			 
			 
			 switch (selecciono)
			 {
			 case 0:
				 sorter.setRowFilter(null);
			 break;
			 
			 case 1:
			
				 sorter.setRowFilter(RowFilter.regexFilter("I",0));
			 break;
				 
			 case 2:
				 sorter.setRowFilter(RowFilter.regexFilter("N",0));
		     break;
			 
			 }
			 
	
	                    
	            }
	        
	}
	
	private void jButton4MouseClicked(MouseEvent evt) {
		System.out.println("jButton4.mouseClicked, event="+evt);
		//TODO add your code for jButton4.mouseClicked
		 if (evt.getClickCount() == 1) {
		
			 

			 
			 int selecciono=listCategory.getSelectedIndex();
			 System.out.println("selecciono:"+selecciono);	
			 String filtro=filtroRapido.getText();
			 
			 
			
		     java.util.List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);  
		     RowFilter<Object,Object> serviceFilter;
		     
			switch (selecciono)
			 {
			 case 0:
				 //filters.add(RowFilter.regexFilter("I",0));  
		            filters.add(RowFilter.regexFilter(filtro,2));  
		            serviceFilter = RowFilter.andFilter(filters);  
		            sorter.setRowFilter(serviceFilter);  
			 break;
			 
			 case 1:
			
				 
				
		            filters.add(RowFilter.regexFilter("I",0));  
		            filters.add(RowFilter.regexFilter(filtro,2));  
		            serviceFilter = RowFilter.andFilter(filters);  
		            sorter.setRowFilter(serviceFilter);  
		 
		
				 
			 break;
				 
			 case 2:
				 filters.add(RowFilter.regexFilter("N",0));  
		            filters.add(RowFilter.regexFilter(filtro,2));  
		            serviceFilter = RowFilter.andFilter(filters);  
		            sorter.setRowFilter(serviceFilter);  
		     break;
			 
			 }
			 
	
		 }
		
		
		
		
		
	}
	
	private void jButton3MouseClicked(MouseEvent evt) {
		System.out.println("jButton3.mouseClicked, event="+evt);
		//TODO add your code for jButton3.mouseClicked
		properties propiedades=new properties(this);
	
		propiedades.setVisible(true);
	}

}
